package cn.nnb.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import cn.nnb.pojo.User;
import cn.nnb.util.Log;

public class UserDao extends BasicDao {

	static {
		TABLE_NAME = "t_user";
	}

	public User login(String uname, String pwd) throws Exception {
		Log.logger.info(uname + "锟斤拷锟节碉拷录");
		System.out.println("uname" + uname + ",pwd" + pwd);
		User user = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put("name", uname);
		map.put("pwd", pwd);
		List<User> list = query(null, map, new IQuery() {

			public List<User> getListFromResultSet(ResultSet resultSet)
					throws SQLException {

				List<User> result = new ArrayList<User>();
				User user;
				while (resultSet.next()) {
					user = new User();
					user.setName(resultSet.getString("name"));
					user.setPwd(resultSet.getString("pwd"));
					user.setRole(resultSet.getString("role"));
					user.setUserno(resultSet.getInt("no"));
					user.setAddress(resultSet.getString("address"));
					user.setTel(resultSet.getString("tel"));
					result.add(user);
				}
				return result;
			}

		});
		if (list.size() == 0) {
			Log.logger.info(uname + "锟斤拷录失锟斤拷");
		} else {
			user = list.get(0);
			Log.logger.info(uname + "锟斤拷录锟缴癸拷");

		}

		// String sql = "select * from t_user  where name = '" + uname +
		// "' and pwd = '" + pwd +"'";
		// this.openconnection(); //锟斤拷菘锟侥达拷
		// Statement st = conn.createStatement();
		// ResultSet rs = st.executeQuery(sql);
		// if(rs != null){
		// while(rs.next()){
		// user = new User();
		// String role = rs.getString("role");
		// user.setName(uname);
		// user.setPwd(pwd);
		// user.setRole(role);
		// Log.logger.info(uname + "锟斤拷录锟缴癸拷");
		// break;
		// }
		// }else{
		// Log.logger.info(uname + "锟斤拷录失锟斤拷");
		// }

		return user;

	}

	/**
	 * 		获取个人信息
	 * @param user
	 * @author Iammeaz
	 * @throws Exception
	 */
	public User getUser(String uno) throws Exception{
		Log.logger.info("正在获取个人信息...");
		Map<String, String> map = new HashMap<String, String>();
		map.put("no", uno);
		List<User> list = query(null, map, new IQuery() {
			public List<User> getListFromResultSet(ResultSet resultSet)
					throws SQLException {

				List<User> result = new ArrayList<User>();
				User user;
				while (resultSet.next()) {
					user = new User();
					user.setName(resultSet.getString("name"));
					user.setPwd(resultSet.getString("pwd"));
					user.setRole(resultSet.getString("role"));
					user.setAddress(resultSet.getString("address"));
					user.setTel(resultSet.getString("tel"));
					user.setTime(resultSet.getString("time"));
					System.out.println(user.getTime());
					result.add(user);
				}
				return result;
			}

		});
		System.out.println(list);
		
		return list.get(0);
	}

	/**
	 * 		注册
	 * @param user
	 * @return
	 * @throws Exception
	 */
	public boolean register(User user) throws Exception {
		Log.logger.info(user.getName() + "正在注册...");

		String[] cols = {"no","name","pwd","address","tel","role","time"};
		String[] values = {this.getN0()+"",user.getName(),user.getPwd(),user.getAddress(),
				user.getTel(),"b",user.getTime()} ;
		
		if(insert(cols, values) > 0){
			Log.logger.info(user.getName() + "注册成功...");
			return true ;
		}else{
			Log.logger.info(user.getName() + "注册失败...[insert(cols, values)失败]");
			return false ;
		}

	}

	/**
	 * //锟叫讹拷锟矫伙拷锟角凤拷锟斤拷锟�
	 * @throws SQLException 
	 * @throws ClassNotFoundException 
	 */
	public boolean UserExist(String uname) throws ClassNotFoundException, SQLException {
		boolean flag = false;
		String sql = "select * from t_user  where name = '" + uname+"'";
		this.openconnection(); //锟斤拷菘锟侥达拷
		Statement st = conn.createStatement();
		ResultSet rs = st.executeQuery(sql);
		System.out.println(sql);
		if(rs.next()){
			flag = true ;
		}else{
			flag = false ;
		}
		return flag ;
	}
	
	public int getN0() throws Exception{
		int no = 0 ;
		String sql = "select max(no) from "+TABLE_NAME;
		this.openconnection();
		Statement st = conn.createStatement();
		ResultSet rs = st.executeQuery(sql);
		rs.next();
		no = rs.getInt(1)+1;
		return no ;
	}

}
